/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lvyh.lightframe.job.admin.controller;

import com.lvyh.lightframe.job.admin.bean.JobUserBean;
import com.lvyh.lightframe.job.Constants;
import com.lvyh.lightframe.job.enums.UserType;
import com.lvyh.lightframe.job.admin.util.RequestUtils;

import javax.servlet.http.HttpServletRequest;

public class BaseController {

    public JobUserBean getLoginUser() {
        return (JobUserBean) RequestUtils.getSession().getAttribute(Constants.SESSION_USER_INFO);
    }

    public String getRequestIp() {
        return RequestUtils.getRequestIp();
    }


    public boolean isLoginSuper() {
        JobUserBean userInfo = getLoginUser();
        if (UserType.SUPER_ADMIN.equals(userInfo.getUserType())) {
            return true;
        } else {
            return false;
        }
    }

    public Long getLoginUserId() {
        if (getLoginUser() == null) {
            return null;
        }
        return Long.parseLong(String.valueOf(getLoginUser().getUserId()));
    }

    public String getLoginName() {
        if (getLoginUser() == null) {
            return null;
        }
        return getLoginUser().getUserName();
    }

    public void setAttribute(String name, Object value) {
        RequestUtils.setAttribute(name, value);
    }

    public String getBasePath(HttpServletRequest request) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
    }

    public String getBasePath() {
        HttpServletRequest request = RequestUtils.getRequest();
        return getBasePath(request);
    }

    public boolean isSuperUser() {
        JobUserBean user = this.getLoginUser();
        return user.getUserType().equals(UserType.SUPER_ADMIN);
    }
}
